package Test;

import java.io.IOException;

import Interact.Interface;
import Tool.Tool;

public class App extends Question {
	/** 题型 */
	public static final int TYPE = 2;
	
	public App(){
		setBody(new AppBody());
		setContent(new Content());
	}
	
	class AnswerMode implements InteractMode{
		@Override
		public void display(Interface inf) throws IOException {
			AppBody body = (AppBody)getBody();
			
			// 打印题干
			inf.println(Tool.toHTML(body.getContent()));
			
			// 打印题干的插图
			body.getIllustration().display(inf);
			
			// 打印答案
			inf.println(String.format(
				"<textarea name=%s>%s</textarea>",
				getKey(), content.getAnswer()));
			
			// 打印评语
			inf.println(Tool.toHTML(content.getComment()));
		}

		@Override
		public void setValueViaInterface(Interface inf) {
			String ans = inf.getValue(getKey());
			content.setAnswer(ans == null ? "" : ans);
		}
	}
	
	class CorrectMode implements InteractMode{
		private String scoreName(){
			return getKey() + "score";
		}
		
		private String commentName(){
			return getKey() + "comment";
		}
		
		@Override
		public void display(Interface inf) throws IOException {
			AppBody body = (AppBody)getBody();
			
			// 打印题干
			inf.println(Tool.toHTML(body.getContent()));
			
			// 打印题干的插图
			body.getIllustration().display(inf);
			
			// 满分
			inf.println("满分：" + body.getGrade());
			
			// 打印答案
			inf.println("学生答案：<br/>" + Tool.toHTML(content.getAnswer()));
			
			// 参考答案
			inf.println("参考答案：<br/>" + Tool.toHTML(body.getReference()));
			
			// 输入得分
			inf.printf("在此输入得分(非法输入认为得分为0)：<input name=\"%s\" value=\"%d\" /><br/>", scoreName(), content.getScore());
			
			// 输入评语
			inf.printf("<textarea name=%s>%s</textarea>",
					commentName(), content.getComment());
		}

		@Override
		public void setValueViaInterface(Interface inf) {
			String value = inf.getValue(scoreName());
			try{content.setScore(Integer.parseInt(value));}
			catch (Exception e){content.setScore(0);}
			
			value = inf.getValue(commentName());
			content.setComment(value == null ? "" : value);
		}
	}

	@Override
	public int getType() {
		return TYPE;
	}
	
	@Override
	void initModes() {
		modes = new InteractMode[] {
			new AnswerMode(),
			new CorrectMode()
		}; 
	}
}
